<?php
  class Plugins_Worktracker extends model {
  
  
  
  function getformadd($productid="")
  {
  
$code  = '<div class="wrpPlg clBg5 clBd2 bdFull">'."\n";
$code .= '<div class="wrpOutAtcPlg">'."\n";
$code .= '<div class="wrpAtcPlg">'."\n";
$code .= '<span class="ico icoFcltPlg"></span>'."\n";
$code .= '<div class="spnAtcPlg clTxtW">'."\n";
$code .= '<span class="txtAtcPlg fs03">Work Tracker</span>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '<div class="wrpTl">'."\n";
$code .= '<div title="Close" class="btnIco closeTl clBd3 mgBtm10 deleterowbutton">'."\n";
$code .= '<span class="ico icoCloseTl"></span>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '<form class="frmFrmPlg clTxt3 fs03">'."\n";
$code .= '<fieldset class="wrpFrmPlg">'."\n";
$code .= '<table class="tbGenInfo tbAddCtnt clTxt3 fs03">'."\n";
$code .= '<tbody><tr>'."\n";
$code .= '<th>Type</th>'."\n";
$code .= '<td>'."\n";
$code .= '<div class="listName">'."\n";
$code .= '<input type="radio" value="1" checked=""  name="typeWrk[]">'."\n";
$code .= '<label for="task">Task</label>'."\n";
$code .= '<input type="radio" value="2"  name="typeWrk[]">'."\n";
$code .= '<label for="problem">Problem</label>'."\n";
$code .= '<input type="radio" value="3"  name="typeWrk[]">'."\n";
$code .= '<label for="Request">Request</label>'."\n";
$code .= '</div>'."\n";
$code .= '</td>'."\n";
$code .= '</tr>'."\n";
$code .= '<tr>'."\n";
$code .= '<th>Name</th>'."\n";
$code .= '<td>'."\n";
$code .= '<input type="text" name="nameWrk[]" class="clBg5 clBd2 fs03 clTxt2">'."\n";
$code .= '</td>'."\n";
$code .= '</tr>'."\n";
$code .= '<tr>'."\n";
$code .= '<th>Assigned To</th>'."\n";
$code .= '<td class="clearfix">'."\n";
$code .= '<input type="text" name="assignWrk[]" class="assignTo clBg5 clBd2 fs03 clTxt2">'."\n";
$code .= '<div class="wrpBtnInpAsgn">'."\n";
$code .= '<div class="clBd2 bdFull btnInpAsgn clBg4 btndropuser">'."\n";
$code .= '<span class="ico icoArwDwnInpTag"></span>'."\n";
$code .= '</div>'."\n";
//
if($_COOKIE['userid'])
{

$sql="SELECT username,simgmid,IF (tb_user.displayname = '', tb_user.username,tb_user.displayname) as displayname FROM (
SELECT * FROM (
select tb_follow.userid ,tb_follow.updatedate FROM  tb_follow where createby=".$_COOKIE['userid']." and tb_follow.status=2 and tb_follow.block=0
UNION
select tb_follow.createby as userid,tb_follow.updatedate from tb_follow where userid=".$_COOKIE['userid']." and tb_follow.status=2 and tb_follow.block=0 ) tb_follow
ORDER BY tb_follow.updatedate desc ) tb_follow 
INNER JOIN tb_user ON tb_user.userid = tb_follow.userid
GROUP BY tb_follow.userid ORDER BY tb_follow.updatedate desc";

$arraydata=$this->db->db_set_recordset($sql);
if(count($arraydata))
{
$code .= '<div class="popInpAsgn clBg5 clBd2 bdFull" style="display:none">'."\n";
foreach($arraydata as $valuedata)
{
$datauser=$this->getimagebyimgmid($valuedata['simgmid']);

$code .= '<div class="lstInpTag clBd2 bdBtm clearfix" >'."\n";
$code .= '<img width="24" height="24" alt="img" src="'.$datauser['newpath'].'" class="genImgUser">'."\n";
$code .= '<span class="txtLstInpTag cutTxt fs03 clTxt2">'.$valuedata['displayname'].'</span>'."\n";
$code .= '<input type="hidden"  value="'.$valuedata['username'].'">'."\n";
$code .= '<div style="display:none" class="txtdisplayname">'.$valuedata['displayname'].'</div>'."\n";
$code .= '</div>'."\n";


}
$code .= '</div>';
}
}

$code .= '</div>'."\n";
$code .= '</td>'."\n";
$code .= '</tr>'."\n";
$code .= '<tr>'."\n";
$code .= '<th>Deadline</th>'."\n";
$code .= '<td>'."\n";
$code .= '<input type="text" name="deadWrk[]" class="clBg5 clBd2 fs03 clTxt2">'."\n";
$code .= '</td>'."\n";
$code .= '</tr>'."\n";
$code .= '<tr>'."\n";
$code .= '<th>Priority</th>'."\n";
$code .= '<td>'."\n";
$code .= '<div class="listName">'."\n";
$code .= '<input type="radio" value="1" checked=""  name="prtWrk[]">'."\n";
$code .= '<label for="low">Low</label>'."\n";
$code .= '<input type="radio" value="2"   name="prtWrk[]">'."\n";
$code .= '<label for="normal">Normal</label>'."\n";
$code .= '<input type="radio" value="3"  name="prtWrk[]">'."\n";
$code .= '<label for="high">High</label>'."\n";
$code .= '</div>'."\n";
$code .= '</td>'."\n";
$code .= '</tr>'."\n";
$code .= '</tbody></table>'."\n";
$code .= '</fieldset>'."\n";
$code .= '<input type="hidden" name="pwksid[]"  value="">'."\n";
$code .= '</form>'."\n";
$code .= '</div>';
  
  
  
  $arraydata['form']=addslashes(str_replace(array("\r", "", "\r"), '', $code));
 // $arraydata['js'][]=pluginjsinfo.'/image.js';
  //$arraydata['js'][]=baseinfo.'/js/jquery.Jcrop.min.js';
 // $arraydata['js'][]=baseinfo.'/js/jquery.iuploader-1.0.js';
  
  
      
  
  
  
  return $arraydata;
  
  
  
  
  
  
  
  }
  
  function changestatusworktracker($pwksid,$productid,$type)
  {
  $arraydata['data']=array();
  
  
    
  
   if($_COOKIE['oauth'])
   {
   $userdata=$this->getuserbyoauth($_COOKIE['oauth']);
   $booleancheck=$this->checkmyproductbyuserid($productid,$userdata['userid']);
   $sql='select * from tb_plugins_worktracker_userid where pwksid='.$pwksid.' and userid='.$userdata['userid'];
   $datacheck=$this->db->db_set_1recordrow($sql);
   if($booleancheck||$userdata['roleid']==3||$datacheck['pwku'])
   {
    
    $text='status';
    if($type=='2')
    {
    $text='testing';
    }
   
    $sql='select '.$text.' from tb_plugins_worktracker_value where pwksid='.$pwksid;
    $arraycheck=$this->db->db_set_1recordrow($sql);
    $check=$userdata['userid'];
    if($arraycheck[$text]!='0')
    {
    $check=0;
    }else
    {
    $sql='select * from tb_plugins_worktracker_userid where pwksid='.$pwksid;
    $arrayworktracker=$this->db->db_set_recordset($sql);
    
    $sql='select name from tb_plugins_worktracker where pwksid='.$pwksid;
    $arrayworktracker2=$this->db->db_set_1recordrow($sql);
    
    if(count($arrayworktracker))
    {
    $arraydatanow=array();
    $notiid=array();
    foreach($arrayworktracker as $valuework)
    {
    
    
            $userid=$valuework['userid'];
           $msgto='" '.$arrayworktracker2['name'].' " are beint test period';
            if($type=='2')
            {
            $msgto='Congratulation " '.$arrayworktracker2['name'].' " is finish';
            }
           $arraydataset['userid']=$userid;
           $arraydataset['createby']=$_COOKIE['userid'];
           $arraydataset['conid']=0;
           $arraydataset['smsgid']=$this->savesupermsg($msgto,$valuework['productid']);
           $notiid[$userid]=$this->savenotificationlist($arraydataset);
    
            $arraydatanow[]=$userid;
           
  
  }
  $this->sendtonotification($arraydatanow,$msgto,$notiid);
    
    }
    
    
    }
   
    $arraysetdata[$text]=$check;
    $this->db->db_set($arraysetdata,'tb_plugins_worktracker_value',' pwksid='.$pwksid);
   
   }
   
   }else
   {
   $arraydata['error'][]=5;
   
   }
   
   return $arraydata;
  
  }
  
  function closeprojectwork($productid)
  {
  $arraydata['data']=array();
  
  
    
  
   if($_COOKIE['oauth'])
   {
   $userdata=$this->getuserbyoauth($_COOKIE['oauth']);
   $booleancheck=$this->checkmyproductbyuserid($productid,$userdata['userid']);
   if($booleancheck||$userdata['roleid']==3)
   {
   
   
    $arraysetdata['close']=1;
    $this->db->db_set($arraysetdata,'tb_product',' productid='.$productid);
   
   }
   
   }else
   {
   $arraydata['error'][]=5;
   
   }
   
   return $arraydata;
  
  }
  
  function getformlanding($arraydata)
  {
  global $logcheck;
  
//  echo "<pre>";
//  print_r($logcheck);
//  echo "</pre>;";
//  
//  echo "<pre>";
//  print_r($arraydata);
//  echo "</pre>;";
//  
//  exit;
$code='';
$str='';
$allow=0;
if($arraydata['mypost'])
{
$allow=1;
}

if(count($arraydata['userlist']))
{
foreach($arraydata['userlist'] as $valueuserlist)
{
if($_COOKIE['userid']==$valueuserlist['userid'])
{
$allow=1;
}
$str.='<a href="'.baseinfo.'/'.$valueuserlist['username'].'" target="_blank">#'.$valueuserlist['displayname'].'</a>  ';

}

}


if($logcheck==1&&$allow==1)
{  
$code .= '<div class="ldIt wrpFrmLd wrpClseWrkTrckLd clBg4">'."\n";
$code .= '<div class="wrpRsv wrpClsWrk">'."\n";
$code .= '<div class="rsvBtnJoin clBgSp2" onclick="closeprojectwork('.$arraydata['productid'].')">'."\n";
$code .= '<span class="clTxtW fs11">CLOSED</span>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '</div>';  

$logcheck++;
}
  
$code .= '<div class="ldIt wrpFrmLd wrpWrkTrckLd clBg4">'."\n";
$code .= '<div class="wrpHdInfoGen mgBtm10">'."\n";
$code .= '<h2 class="clTxt2 fs04 boldGen">Work Tracker</h2>'."\n";
$code .= '</div>'."\n";
$code .= '<table class="tbGenInfo tbFrmLd clTxt3 fs03">'."\n";
$code .= '<tbody>'."\n";

$code .= '<tr><th>Type</th>'."\n";

$type='Task';
switch ($arraydata['typeid']) {
case '1':
$type='Task';
break;
case '2':
$type='Problem';
break;
case '3':
$type='Request';
break;

}


$code .= '<td>'.$type.'</td>'."\n";


$code .= '</tr>'."\n";

$code .= '<tr>'."\n";
$code .= '<th>Name</th>'."\n";
$code .= '<td>'.$arraydata['name'].'</td>'."\n";
$code .= '</tr>'."\n";

$code .= '<tr>'."\n";
$code .= '<th>SKU</th>'."\n";
$code .= '<td>'.$arraydata['pwksid'].'</td>'."\n";
$code .= '</tr>'."\n";
$code .= '<tr>'."\n";
$code .= '<th>Assigned To</th>'."\n";



$code .= '<td>'.$str.'</td>'."\n";


$code .= '</tr>'."\n";
$code .= '<tr>'."\n";
$code .= '<th>Deadline</th>'."\n";



$code .= '<td>'.$arraydata['newdate'].'</td>'."\n";
$code .= '</tr>'."\n";
$code .= '<tr>'."\n";
$code .= '<th>Priority</th>'."\n";

$type='Low';
switch ($arraydata['priority']) {
case '1':
$type='Low';
break;
case '2':
$type='Normal';
break;
case '3':
$type='High';
break;

}

$code .= '<td>'.$type.'</td>'."\n";
$code .= '</tr>'."\n";

$code .= '<tr>'."\n";
$code .= '<th>Status</th>'."\n";
$code .= '<td>'."\n";
$checked='';
$text='Not Done';
if($arraydata['status']!='0')
{
$text='Done';
$checked='checked="checked"';
}
if($allow)
{
$code .= '<input type="checkbox" '.$checked.' class="inpCbWrk done-'.$arraydata['pwksid'].'" onclick="changestatusworktracker('.$arraydata['pwksid'].','.$arraydata['productid'].',1)" >'."\n";
}
$code .= '<label for="donework">'.$text.'</label>'."\n";
$code .= '</td>'."\n";
$code .= '</tr>'."\n";

$code .= '<tr>'."\n";
$code .= '<th>Testing</th>'."\n";
$code .= '<td>'."\n";

$checked='';
$text='Not TEST';
if($arraydata['testing']!='0')
{
$text='TESTED';
$checked='checked="checked"';
}
if($allow)
{
$code .= '<input type="checkbox" '.$checked.' class="inpCbWrk tested-'.$arraydata['pwksid'].'" onclick="changestatusworktracker('.$arraydata['pwksid'].','.$arraydata['productid'].',2)"  >'."\n";
}
$code .= '<label for="tested">'.$text.' (For Tester)</label>'."\n";
$code .= '</td>'."\n";
$code .= '</tr>'."\n";

$code .= '</tbody></table>'."\n";
$code .= '</div>';

$data['str']=$code;
$data['target']='secondtarget';
  
  return $data; 
  
  
  }
  
   function getplugin($plugreid)
  {
  
 
     $sql="SELECT
tb_plugins_worktracker.typeid,
tb_plugins_worktracker.`name`,
tb_plugins_worktracker.pwkid,
tb_plugins_worktracker_value.testing,
tb_plugins_worktracker_value.`status`,
tb_plugins_worktracker.enddate,
tb_plugins_worktracker.priority,
tb_plugins_worktracker.productid,
DATE_FORMAT(tb_plugins_worktracker.enddate
,'%d/%m/%Y') newdate,
tb_plugins_worktracker.pwksid
FROM
tb_plugins_worktracker
INNER JOIN tb_plugins_worktracker_value ON tb_plugins_worktracker.pwksid = tb_plugins_worktracker_value.pwksid 
where tb_plugins_worktracker.plugreid=".$plugreid;
     $arrayplugin=$this->db->db_set_1recordrow($sql);
     
     
     $sql="
SELECT
tb_user.userid,username,IF (tb_user.displayname = '', tb_user.username,tb_user.displayname) as displayname
FROM
tb_plugins_worktracker_userid
INNER JOIN tb_user ON tb_plugins_worktracker_userid.userid = tb_user.userid
where tb_plugins_worktracker_userid.pwksid=".$arrayplugin['pwksid']."
";
     $arrayplugin['userlist']=$this->db->db_set_recordset($sql);
     if($_COOKIE['userid'])
     {
     $arrayplugin['mypost']=$this->checkmyproductbyuserid($arrayplugin['productid'],$_COOKIE['userid']);
     }else
     {
     $arrayplugin['mypost']=false;
     }
     

  
     return $arrayplugin;

  
  
  

  
  }
  function saveplugin($arraydata,$productid,$plugreid)
  {

  
  if($arraydata['enddate'])
  {
  list($day,$month,$year)=explode("/",$arraydata['enddate']);
  
  $arraydata['enddate']=$year.'-'.$month.'-'.$day;
  
  }

  $arraydata['productid']=$productid;
  $arraydata['plugreid']=$plugreid;
  $arraydata['createdate']=date("Y-m-d H:i:s");
  $pwksid=$arraydata['pwksid'];
  if($pwksid)
  {
    $sql='DELETE FROM tb_plugins_worktracker_userid WHERE pwksid='.$pwksid;
    $this->db->db_delete($sql);
  }
  $assign=$arraydata['assign'];
  unset($arraydata['assign']);
 // $plugreid   assign
  
  if($pwksid)
  {
 
  $this->db->db_set($arraydata,'tb_plugins_worktracker');
  }else
  {
  
  $arraydata2['productid']=$productid;
  $arraydata2['status']=0;
  $arraydata2['testing']=0;
  $this->db->db_set($arraydata2,'tb_plugins_worktracker_value');
  $pwksid=$this->db->db_get_last_number();
  
  $arraydata['pwksid']=$pwksid;
  $this->db->db_set($arraydata,'tb_plugins_worktracker');
  
  
  
  
  }
  
  if($assign)
  {
  $arraydata3=explode("#",$assign);
  $arraynew=array();
  if(count($arraydata3))
  {
  foreach($arraydata3 as $valuedata3)
  {
  $str=trim($valuedata3);
  if($str)
  {
  
  $arraynew[]=$str;
  
  }
  
  }
  array_unique($arraynew);
  if(count($arraynew))
  {
   
  $strwhereproductid=join("','",$arraynew); 
  $sql="select userid from tb_user where username IN('".$strwhereproductid."')";
  $arraypro=$this->db->db_set_recordset($sql);
  $arraycheck=array();
  if(count($arraypro))
  {
  foreach($arraypro as $valuepro)
  {
  $arraycheck['userid']=$valuepro['userid'];
  $arraycheck['productid']=$productid;
  $arraycheck['pwksid']=$pwksid;
  $this->db->db_set($arraycheck,'tb_plugins_worktracker_userid');
  
           $userid=$arraycheck['userid'];
           $msgto='You have been assign to " '.$arraydata['name'].' "';
           $arraydataset['userid']=$userid;
           $arraydataset['createby']=$_COOKIE['userid'];
           $arraydataset['conid']=0;
           $arraydataset['smsgid']=$this->savesupermsg($msgto,$productid);
           $notiid[$userid]=$this->savenotificationlist($arraydataset);
           
           
           $arraydatanow[]=$userid;
           
  
  }
  $this->sendtonotification($arraydatanow,$msgto,$notiid);
  }
  
  }
  
  
  }
  
  }
  
  
  }
  
  
  }
?>
